package com.zznj.xzjc.fragment.index;

import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxui.fragment.NewLazyFragment;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.PushAdapter;
import com.zznj.xzjc.bean.index.PushData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：知识库
 */
public class AjcxDsjjsFragment extends NewLazyFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;

    @BindView(R.id.tishiImg)
    ImageView tishiImg;

    @BindView(R.id.bottomLinear)
    View bottomLinear;

    Unbinder unbinder;

    private int readType = 1; //1--未读；2--已读

    public static AjcxDsjjsFragment getInstance(int readType) {
        AjcxDsjjsFragment fragment = new AjcxDsjjsFragment();
        fragment.readType = readType;
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.gyss_fragment_index_push_layout;
    }

    private LoginBean user;

    public void search() {
        refresh();
    }

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        init();
        initSrl();
        super.initView(view);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshData(MessageEvent messageEvent) {
        if (messageEvent.getTag().equals(EventTag.DT)) {
            refresh();
        }
    }

    public void refresh() {
        isShowDialog = false;
        page = 1;
        srlAll.setEnableLoadMore(true);
        mList.clear();
        bottomLinear.setVisibility(View.GONE);
        mAdapter.notifyDataSetChanged();
        getData();
    }


    private void initSrl() {
        srlAll.setEnableRefresh(true);
        srlAll.setEnableLoadMore(true);
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh();
        });
        srlAll.setOnLoadMoreListener(refresh_layout -> {
            isShowDialog = false;
            page++;
            getData();
        });
    }

    private List<PushData> mList;
    private PushAdapter mAdapter;

    private void init() {
        mList = new ArrayList<>();
        ((SimpleItemAnimator) recMain.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recMain.setLayoutManager(linearLayoutManager);
        mAdapter = new PushAdapter(R.layout.gyss_item_push, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }

    private int page = 1;
    private boolean isShowDialog = true;

    @Override
    protected void initData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        getData();
    }

    private void getData() {
        SubscriberOnNextListener<BaseBean<List<PushData>>> onNextListener = baseBean -> {
            if(srlAll != null){
                srlAll.finishLoadMore();
                srlAll.finishRefresh();
            }
            if(mList.size() + baseBean.getResult().size() > 0 && baseBean.getResult().size() < 10){
                bottomLinear.setVisibility(View.VISIBLE);
                if(srlAll != null){
                    srlAll.setEnableLoadMore(false);
                }
            }
            mList.addAll(baseBean.getResult());
            mAdapter.notifyDataSetChanged();

            if(mAdapter.getItemCount() == 0){
                tishiImg.setVisibility(View.VISIBLE);
                srlAll.setVisibility(View.GONE);
                tishiImg.setImageResource(R.mipmap.gyss_zwnr_img);
            }else{
                tishiImg.setVisibility(View.GONE);
                srlAll.setVisibility(View.VISIBLE);
            }

        };
        if(mList.size() == 0){
            page = 1;
        }
        FormBody.Builder requestBuilder = new FormBody.Builder()
                .add("readStatus", readType+"")
                .add("page",page+"");
        FormBody requestBody = requestBuilder.build();
        HttpMethods.getInstance().getMessageList(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}